package com.itcast.proxy;

import java.io.FileOutputStream;
import java.io.IOException;

import sun.misc.ProxyGenerator;

public class ProxyClassFileGenerator {
	/**
	 * 生成动态代理类的字节码.class文件的工具类
	 * @param className 生成的.class文件的名称
	 * @param filePath  生成的.class文件存放的路径
	 * @param cls 生成的动态代理类需要实现的接口
	 */
	@SuppressWarnings("rawtypes")
	public static void writeObjectToFile(String className,String filePath, Class[] cls) {
		byte[] classFile = ProxyGenerator.generateProxyClass(className, cls);
		FileOutputStream out = null;
		try {
			filePath = filePath+filePath + "" + ".class";
			out = new FileOutputStream(filePath);
			out.write(classFile);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null)
					out.close();
			} catch (IOException e) {
				
			}
		}
	}
}
